package com.tsky.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * c3p0管理数据库连接
 * @author tsky
 *
 */
public final class DataSourceManager {
	// 保存数据库连接池的容器
	public static HashMap<String,ComboPooledDataSource> dataSourceMap = new HashMap<String,ComboPooledDataSource>();
	
	// 数据库配置信息
	private static Properties pp = null;
	private static InputStream is = null;
	private static final String FILENAME = "dbInfo.properties";
	
	public DataSourceManager(){}
	
	/**
	 * 从连接池容器返回连接池对象
	 * @param dataSourceName
	 * @return
	 */
	public static ComboPooledDataSource getDataSource(String dataSourceName){
		ComboPooledDataSource ds = null;
		if(!dataSourceMap.containsKey(dataSourceName.toString())){
			try{
				ds = new ComboPooledDataSource();
				// read properties file
				pp = new Properties();
				is = DataSourceManager.class.getClassLoader().getResourceAsStream(FILENAME);
				pp.load(is);
				//配置数据源
	             ds.setDriverClass(pp.getProperty(dataSourceName + "." + "Driver").trim());
	             ds.setJdbcUrl(pp.getProperty(dataSourceName + "." + "Url").trim());
	             ds.setUser(pp.getProperty(dataSourceName + "." + "User").trim());
	             ds.setPassword(pp.getProperty(dataSourceName + "." + "Password").trim());
	             ds.setMaxPoolSize(Integer.parseInt(pp.getProperty(dataSourceName + "." + "MaxPoolSize").trim()));
	             ds.setMinPoolSize(Integer.parseInt(pp.getProperty(dataSourceName + "." + "MinPoolSize").trim()));
	             ds.setMaxIdleTime(Integer.parseInt(pp.getProperty(dataSourceName + "." + "MaxIdleTime").trim()));
	             ds.setInitialPoolSize(Integer.parseInt(pp.getProperty(dataSourceName + "." + "InitialPoolSize").trim()));
	             ds.setAcquireIncrement(Integer.parseInt(pp.getProperty(dataSourceName + "." + "AcquireIncrement").trim()));
	             ds.setAcquireRetryAttempts(Integer.parseInt(pp.getProperty(dataSourceName + "." + "AcquireRetryAttempts").trim()));
	             ds.setAcquireRetryDelay(Integer.parseInt(pp.getProperty(dataSourceName + "." + "AcquireRetryDelay").trim()));
	             ds.setMaxStatements(Integer.parseInt(pp.getProperty(dataSourceName + "." + "MaxStatements").trim()));
	             ds.setIdleConnectionTestPeriod(Integer.parseInt(pp.getProperty(dataSourceName + "." + "IdleConnectionTestPeriod").trim()));
	             ds.setCheckoutTimeout(Integer.parseInt(pp.getProperty(dataSourceName + "." + "CheckoutTimeout").trim()));
	             ds.setTestConnectionOnCheckin(Boolean.parseBoolean(pp.getProperty(dataSourceName + "." + "TestConnectionOnCheckin").trim()));
	             ds.setTestConnectionOnCheckout(Boolean.parseBoolean(pp.getProperty(dataSourceName + "." + "TestConnectionOnCheckout").trim()));
				
				// 将数据源存入连接池容器
				dataSourceMap.put(dataSourceName, ds);
				return ds;
			}catch(Exception e){
				e.printStackTrace();
				throw new RuntimeException("无法根据配置文件创建连接池对象",e);
			}finally{
				try{
					is.close();
				}catch(Exception e){
					e.printStackTrace();
					throw new RuntimeException("无法找到配置文件",e);
				}
			}
		}else{
			return (ComboPooledDataSource)dataSourceMap.get(dataSourceName.toString());
		}
	}
	
	public static void main(String[] args) {
		System.out.println(DataSourceManager.getDataSource("test").hashCode());
	}
}
